package org.bemo.config;

import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.blade.Blade;
import com.blade.annotation.Order;
import com.blade.config.BaseConfig;
import com.blade.config.Configuration;
import com.blade.ioc.annotation.Component;
import com.blade.jdbc.ActiveRecord;
import com.blade.jdbc.ar.SampleActiveRecord;

/**
 * 数据库配置
 * 
 * @author 王杰
 *
 */
@Component
@Order(sort = 1)
public class DBConfig implements BaseConfig {

	// 数据库操作对象
	public ActiveRecord activeRecord;

	private Logger LOGER = LoggerFactory.getLogger(DBConfig.class);

	@Override
	public void config(Configuration arg0) {
		try {
			// 加载数据库配置信息
			InputStream in = DBConfig.class.getClassLoader().getResourceAsStream("druid.properties");
			// 创建配置文件对象
			Properties properties = new Properties();
			properties.load(in);
			// 创建数据库连接对象
			DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
			activeRecord = new SampleActiveRecord(dataSource);
			// 数据库配置对象放入主配置对象
			Blade.$().ioc().addBean(activeRecord);
			LOGER.info("加载数据库信息成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
